package com.szt.mobile.inventory.modules.inventory.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.szt.mobile.inventory.R;

/**
 * Created by yanzhaodi on 2017/11/16.
 */

public class ChooseDateDialog extends Dialog implements View.OnClickListener {
    public ChooseDateDialog(@NonNull Context context, String date) {
        this(context, R.style.CommonDialog);
        this.date = date;
    }

    private ChooseDateDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    private String date;
    private DateCallback callback;
    private DatePicker datePicker;
    private TextView cancelTv;
    private TextView clearTv;
    private TextView settingTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_choose_date);

        datePicker = (DatePicker) findViewById(R.id.date_picker);
        cancelTv = (TextView) findViewById(R.id.tv_choose_date_cancel);
        clearTv = (TextView) findViewById(R.id.tv_choose_date_clear);
        settingTv = (TextView) findViewById(R.id.tv_choose_date_setting);

        cancelTv.setOnClickListener(this);
        clearTv.setOnClickListener(this);
        settingTv.setOnClickListener(this);

        String[] ss = date.split("-");
        if (ss.length == 3) {
            datePicker.init(Integer.valueOf(ss[0]), Integer.valueOf(ss[1]) - 1, Integer.valueOf(ss[2]), null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_choose_date_cancel:
                dismiss();
                break;
            case R.id.tv_choose_date_clear:
                if (null != callback) {
                    callback.onChoose("");
                }
                dismiss();
                break;
            case R.id.tv_choose_date_setting:
                if (null != callback) {
                    int year = datePicker.getYear();
                    int month = datePicker.getMonth() + 1;
                    int day = datePicker.getDayOfMonth();

                    StringBuilder sb = new StringBuilder(String.valueOf(year));
                    sb.append("-");
                    if (month > 9) {
                        sb.append(String.valueOf(month));
                    } else {
                        sb.append("0").append(String.valueOf(month));
                    }
                    sb.append("-");
                    if (day > 9) {
                        sb.append(String.valueOf(day));
                    } else {
                        sb.append("0").append(String.valueOf(day));
                    }

                    callback.onChoose(sb.toString());
                }
                dismiss();
                break;
            default:
                break;
        }
    }

    public void setDateCallback(DateCallback callback) {
        this.callback = callback;
    }

    public interface DateCallback {
        void onChoose(String date);
    }
}
